<?php
/**
 * @file ct.autotag.updateNOTAG.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-04-30 02:04:27 +0000 (Tue, 30 Apr 2013) $
 *
 * $Id: ct.autotag.updateNOTAG.php 63 2013-04-30 02:04:27Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0-BETA1/cron/ct.autotag.updateNOTAG.php $
 */

$wcCronScripts["autotag.updateNOTAG"] = array(
	'auto' => true,
	'enabled' => true,
	'name' => 'NOTAG entries update',
	'class' => 'WCTagsUpdateNOTAG',
	'url' => "http://code.google.com/p/wcomix/"
);

if(!isset($wcDefaults["class-WCTagsUpdateNOTAG"])) {
	$wcDefaults["class-WCTagsUpdateNOTAG"] = array();
}

class WCTagsUpdateNOTAG extends WCCronTool {
	public static $TagName = WC_SPECIAL_TAG_UNTAGGED;

	public function run($spacer = "") {
		global $wcTagsHolder;
		global $wcSpecialTags;
		global $wcGroupsHolder;
		global $wcDefaults;
		global $wcDatabaseAccess;

		$db = WCConnection::Instance();
		$dbPrefix = $wcDatabaseAccess['prefix'];

		foreach($wcGroupsHolder->groups() as $group) {
			if(is_readable($group->root)) {
				echo "{$spacer}Checking group '{$group->name}' ({$group->id}):\n";

				$tag = $wcTagsHolder->itemByName(WCTagsUpdateNOTAG::$TagName, $group);
				if(!$tag) {
					$tag = $wcTagsHolder->create(WCTagsUpdateNOTAG::$TagName, $group);
					if(!$tag) {
						trigger_error(__CLASS__."::".__FUNCTION__."(): Unable to create tag '".WCTagsUpdateNOTAG::$TagName."' for group '{$group->name}' ({$group->id})", E_USER_ERROR);
					}
				}

				//
				// Removing tagged comics
				$stmt = null;
				$stmtId = __CLASS__."::".__FUNCTION__."[rselect]";
				if($db->has($stmtId)) {
					$stmt = $db->get($stmtId);
				} else {
					$query = "select  mem_comic,\n";
					$query .= "        count(*) as mem_count\n";
					$query .= "from    {$dbPrefix}tag_members\n";
					$query .= "                inner join {$dbPrefix}comics\n";
					$query .= "                        on(mem_comic = cmc_id and mem_group = cmc_group)\n";
					$query .= "where   mem_comic in (\n";
					$query .= "                select  mem_comic\n";
					$query .= "                from    {$dbPrefix}tag_members\n";
					$query .= "                where   mem_tag   = :tag\n";
					$query .= "                 and    mem_group = :group)\n";
					$query .= " and    mem_group = :group\n";
					$query .= "group by mem_comic\n";
					$stmt = $db->prepare($stmtId, $query);
				}

				$params = array(
					":group" => $group->id,
					":tag" => $tag->id
				);

				if($stmt->execute($params) && $stmt->rowCount() > 0) {
					foreach($stmt->fetchAll() as $row) {
						if($row["mem_count"] > 1) {
							global $wcComicsHolder;

							$comic = $wcComicsHolder->item($row["mem_comic"], $group);
							if($comic && $comic->ok()) {
								echo "{$spacer}\tRemoving '{$comic->name}' ({$comic->id}@{$comic->directory}) from '{$tag->name}'\n";
								$tag->removeComic($comic);
							}
						}
					}
				}

				//
				// Adding tag
				$stmt = null;
				$stmtId = __CLASS__."::".__FUNCTION__."[add]";
				if($db->has($stmtId)) {
					$stmt = $db->get($stmtId);
				} else {
					$query = "select  *\n";
					$query .= "from    {$dbPrefix}comics\n";
					$query .= "where   cmc_id not in (\n";
					$query .= "                select  distinct mem_comic\n";
					$query .= "                from    {$dbPrefix}tag_members\n";
					$query .= "                where   mem_group = :group)\n";
					$query .= " and    cmc_group = :group\n";
					$query .= " and    cmc_lost  = 'N'\n";
					$stmt = $db->prepare($stmtId, $query);
				}

				$params = array(":group" => $group->id);

				if($stmt->execute($params)) {
					foreach($stmt->fetchAll() as $row) {
						global $wcComicsHolder;

						$comic = $wcComicsHolder->item($row["cmc_id"], $group);
						if($comic && $comic->ok()) {
							echo "{$spacer}\tAdding '{$comic->name}' ({$comic->id}@{$comic->directory}) to '{$tag->name}'\n";
							$tag->add($comic);
						}
					}
				}
			}
		}
	}
	public function showHelp($spacer = "") {
		global $wcSpecialTags;
		echo "{$spacer}This tool adds tag '".self::$TagName."' to any comics without tags.\n\n";
		echo "{$spacer}It requires no extra parameters.\n";
	}
	protected function loadOptions() {
		$this->_optionsLoaded = true;
	}
}
?>
